﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ccit.Utils;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Text;
using Ccit.OA.MsGroup;

namespace Ccit.OA.Group
{
    public partial class group_personal_tree : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["Search"] = "";
                _hidType.Value = (Request.QueryString["type"] != null) ? Request.QueryString["type"].ToString() : "";                
                initializePage(); 
            }
        } 
        /// <summary>
        /// 初始化页面
        /// </summary>
        private void initializePage()
        {
            createTree();
            trvDepAddress.ExpandLevel = 1;
            string iNode = "";
            string[] NodeIndex = null;
            string Search = (ViewState["Search"] != "") ? "1" : "0";
            if (Search != "0")
            {
                trvDepAddress.SelectedNodeIndex = "0.0";
                iNode = trvDepAddress.Nodes[0].Nodes[0].ID;
                NodeIndex = iNode.Split('_');
                _hidUpAddrId.Value = NodeIndex[0].ToString(); 
            }
        }
        private void createTree()
        {
            /* c创建根节点 */
            Microsoft.Web.UI.WebControls.TreeNode oRoot = new Microsoft.Web.UI.WebControls.TreeNode();
            ///调用创建根节点方法
            createRootNode(ref oRoot);
            ///添加第一层节点
            trvDepAddress.Nodes.Add(oRoot);
            ///添加节点信息
            createSubNodes(ref oRoot);
        }
        /// <summary>
        /// 创建根节点
        /// </summary>
        /// <param name="oRoot"></param>
        private void createRootNode(ref Microsoft.Web.UI.WebControls.TreeNode oRoot)
        {
            if (_hidType.Value.ToString() == "0")
            {
                oRoot.Text = "人员分组";
            }
            else
            {
                oRoot.Text = "系统分组";
            }
            oRoot.ImageUrl = "../images/toolbar/Folder_open.gif";
            oRoot.NavigateUrl = "";
            oRoot.Target = "";
            oRoot.ID = "0_0";
        }
        /// <summary>
        /// 节点信息
        /// </summary>
        /// <param name="oRoot"></param>
        private void createSubNodes(ref Microsoft.Web.UI.WebControls.TreeNode oRoot)
        {
            GroupBLL Bll = new GroupBLL();
            string strAddrXML = Bll.GetGroupMes(_hidType.Value);   // get one stair ast type 
            Microsoft.Web.UI.WebControls.TreeNode oTreeNode;
            XElement XeAddr = XElement.Parse(strAddrXML);
            string groupId = "";
            string groupName = "";
            var eAddrArray = from eAddr in XeAddr.Elements("ADDR") select eAddr;

            foreach (var eAddr in eAddrArray)
            {
                groupId = eAddr.Element("GROUPID").Value;
                groupName = eAddr.Element("GROUPNAME").Value;
                oTreeNode = createTreeNode(groupName, groupId);
                ViewState["Search"] = groupId;
                oRoot.Nodes.Add(oTreeNode);
            }
            XeAddr = null;
            Bll = null;
        }
        /// <summary>
        /// 创建节点信息
        /// </summary>
        /// <param name="addrName">节点名称</param>
        /// <param name="addrId">节点id</param>
        /// <param name="hasChild">子节点id</param>
        /// <param name="state">状态</param>
        /// <returns></returns>
        private Microsoft.Web.UI.WebControls.TreeNode createTreeNode(string groupName, string groupId)
        {
            Microsoft.Web.UI.WebControls.TreeNode oNode = new Microsoft.Web.UI.WebControls.TreeNode();
            oNode.Text = groupName;
            string sParam = "GroupId=" + groupId;            
            string encodeParam = Base64Lib.Encode(sParam);
            oNode.NavigateUrl = "group_personal_list.aspx?" + encodeParam;
            oNode.ImageUrl = "../images/toolbar/filelogo.gif";
            oNode.Target = "iFrmEdit";
            oNode.ID = groupId;
            oNode.Type = "root";
            return oNode;
        }
        /// <summary>
        /// 删除节点信息
        /// </summary>
        /// <param name="addrId"></param>
        /// <param name="nodeIndex"></param>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string DeleteGroup(string groupId, string nodeIndex)
        {
            StringBuilder sb = new StringBuilder("<RESULT>");
            const string cnsOperType = "ADDRDELETE";

            GroupBLL BLL = new GroupBLL();
            List<string> lstResult = BLL.DoOperate(groupId,"","0");
            var newObj = new { resultSign = lstResult[0], message = lstResult[1] };
            string serExpr = JavaScriptConvert.SerializeObject(newObj);
            sb.Append("<NODEINDEX>").Append(nodeIndex).Append("</NODEINDEX>");
            sb.Append("<OPERTYPE>").Append(cnsOperType).Append("</OPERTYPE>");
            sb.Append("<EXERESULT><![CDATA[").Append(serExpr).Append("]]></EXERESULT>");
            sb.Append("</RESULT>");
            BLL = null;
            return sb.ToString();
        }
        #region Ajax Methods
        [global::System.Web.Services.WebMethod]
        public static string DoSaveDetail(string strJson)
        {
            GroupBLL Bll = new GroupBLL();
            MsGroupDetail ms = (MsGroupDetail)JavaScriptConvert.DeserializeObject(strJson, typeof(MsGroupDetail));
            List<string> lstResult = Bll.DoSaveDetail(ms);
            var newObj = new { resultSign = lstResult[0], message = lstResult[1] };
            string serExpr = JavaScriptConvert.SerializeObject(newObj);
            Bll = null;
            newObj = null;
            return serExpr;
        }
        #endregion
    }
}
